package com.htc.model.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.RetailersController;
import com.htc.model.dto.RetailersDTO;
import com.htc.utils.CommonObjects;

public class RetailerModel {
	private ActionEvent actionEvent;
	public static RetailerModel instance;

	public static RetailerModel getInstance() {
		if (instance == null)
			instance = new RetailerModel();
		return instance;
	}

	public void handleControllerEvent(ActionEvent e) {
		this.actionEvent = e;
		switch (e.action) {
		case Constants.GET_RETAILER:
			ArrayList<RetailersDTO> news = getAllRetailer((Context) e.sender);
			e.viewData = news;
			RetailersController.getInstance().handleModelViewEvent(e);
			break;

		default:
			break;
		}
	}

	public ArrayList<RetailersDTO> getAllRetailer(Context context) {
		ArrayList<RetailersDTO> arrData = new ArrayList<RetailersDTO>();
		String sql = "SELECT * FROM retailer";
		DBHelper db = new DBHelper(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			RetailersDTO dto = new RetailersDTO();
			dto.setRetailerId(c.getString(c.getColumnIndex("retailer_id")));
			dto.setRetailerName(c.getString(c.getColumnIndex("retailer_name")));
			dto.setRetailerDescription(c.getString(c
					.getColumnIndex("retailer_description")));
			dto.setRetailerImage(c.getString(c.getColumnIndex("retailer_image")));
//			dto.setRetailerBitmapImage(CommonObjects.getBitmapImg(c.getString(c
//					.getColumnIndex("retailer_image"))));
			dto.setRetailerAddedDate(c.getString(c.getColumnIndex("added_date")));
			dto.setRetailerPriority(c.getString(c.getColumnIndex("priority")));
			dto.setRetailerNameAr(c.getString(c
					.getColumnIndex("retailer_name_ar")));
			dto.setRetailerDescriptionAr(c.getString(c
					.getColumnIndex("retailer_description_ar")));
			arrData.add(dto);
		}
		c.close();
		db.close();
		return arrData;
	}

	public boolean checkDB(Context context, String id) {
		boolean flag = false;
		String sql = "SELECT retailer_id FROM " + Constants.TBL_RETAILER
				+ " where retailer_id='" + id + "'";
		DBHelper db = DBHelper.getInstance(context);
		Cursor c = db.Excutequery(sql);
		while (c.moveToNext()) {
			flag = true;
			break;
		}
		c.close();
		db.close();
		return flag;
	}

	public boolean insertRetailer(RetailersDTO ret, Context context) {
		boolean flag = false;
		if (!checkDB(context, ret.getRetailerId())) {
			String DATABASE_TABLE = Constants.TBL_RETAILER;
			ContentValues values = new ContentValues();
			values.put("retailer_id", ret.getRetailerId());
			values.put("retailer_name", ret.getRetailerName());
			values.put("retailer_description", ret.getRetailerDescription());
			values.put("retailer_image", ret.getRetailerImage());
			values.put("added_date", ret.getRetailerAddedDate());
			values.put("priority", ret.getRetailerPriority());
			values.put("retailer_name_ar", ret.getRetailerNameAr());
			values.put("retailer_description_ar",
					ret.getRetailerDescriptionAr());
			DBHelper db = DBHelper.getInstance(context);
			flag = db.ExcuteInsert(null, DATABASE_TABLE, values);
			db.close();
		}

		return flag;
	}

	public boolean updateRetailer(RetailersDTO ret, Context context) {
		boolean flag = false;

		String DATABASE_TABLE = Constants.TBL_RETAILER;
		String where = "retailer_id='" + ret.getRetailerId() + "'";
		ContentValues values = new ContentValues();

		values.put("retailer_name", ret.getRetailerName());
		values.put("retailer_description", ret.getRetailerDescription());
		values.put("retailer_image", ret.getRetailerImage());
		values.put("added_date", ret.getRetailerAddedDate());
		values.put("priority", ret.getRetailerPriority());
		values.put("retailer_name_ar", ret.getRetailerNameAr());
		values.put("retailer_description_ar", ret.getRetailerDescriptionAr());
		DBHelper db = DBHelper.getInstance(context);
		flag = db.ExcuteUpdate(null, DATABASE_TABLE, values, where, null);
		db.close();
		return flag;
	}

	public void deleteRetailer(RetailersDTO ret, Context context) {
		String sql = "retailer_id='" + ret.getRetailerId() + "'";
		DBHelper db = DBHelper.getInstance(context);
		String DATABASE_TABLE = Constants.TBL_RETAILER;
		db.deleteTable(DATABASE_TABLE, sql);
		db.close();
	}

}
